package com.lx.commonlib.mvp.view.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lx.commonlib.R;
import com.lx.commonlib.mvp.persenter.BasePresenter;
import com.lx.commonlib.mvp.view.activity.BaseActivity;
import com.lx.commonlib.utils.ui.DrawableUtil;
import com.lx.commonlib.utils.ui.ResourcesUtil;


/**
 * Created by Sight-WXC on 2016/8/27 0027.
 * 带Toolbar的Fragment,遵循MD风格设计
 * 可以随时设置 标题栏
 */
public abstract class ToolbarFragment<S extends BaseActivity, T extends BasePresenter> extends
        BaseFragment<S,T> {

    /**
     * MD标题栏
     */
    protected RelativeLayout mToolbar;
    /**
     * 是否隐藏
     */
    protected boolean mIsHidden = false;
    /**
     * 设置标题
     */
    private TextView mTvTittle;
    /**
     * all fragment root
     */
    public FrameLayout mRootFragmentLayout;

    /**  返回按钮 */
    private ImageView mIvBack;

    protected TextView tvRight;

    @Override
    protected void onCreateFragment(Bundle savedInstanceState) {
        //设置内容布局
        int toolbarLayoutId=getToolBarLayoutId();
        if (toolbarLayoutId!=0){
            setContentView(toolbarLayoutId);
            initToolBarView();
        }else {
            setContentView(R.layout.fragment_toolbar);
            //查找控件
            mToolbar = (RelativeLayout) findViewById(R.id.toolbar);
            mTvTittle = (TextView) findViewById(R.id.tv_root_title);
            tvRight = findViewById(R.id.tv_right_text);

            mRootFragmentLayout = (FrameLayout) findViewById(R.id.flt_toolbar_content_container);
            //填充子view的内容布局并适配
            int contentLayoutId = getContentLayoutId();
            if(contentLayoutId!=0){
                View view = mLayoutInflater.inflate(contentLayoutId, mRootFragmentLayout, false);
                mRootFragmentLayout.addView(view);
            }

            mIvBack = (ImageView) findViewById(R.id.iv_back);
            //初始化返回按钮
            if (isNeedBack()){
                mIvBack.setVisibility(View.VISIBLE);
                mIvBack.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getActivity().onBackPressed();
                    }
                });
            }else{
                mIvBack.setVisibility(View.INVISIBLE);
            }
        }

    }


    /**
     * 设置toolbar布局
     * 返回键等等的id 请和 R.layout.fragment_toolbar  一样
     * @return ToolBarLayoutId
     */
    protected  @LayoutRes  int getToolBarLayoutId(){
        return 0;
    }

    /**
     * 自定义toolbar需要自己初始化view
     */
    protected void initToolBarView(){

    }


    /**
    * 方法说明:  获取子fragment的内容布局Id
    * created by liuxiong on 2017/11/15 10:12
    */
    protected abstract @LayoutRes int getContentLayoutId();


    /**
    * 方法说明:  是否需要返回
    * created by liuxiong on 2017/11/15 10:05
    */
    public  boolean isNeedBack(){
        return true;
    }

    /**
     * 隐藏 Toolbar
     */
    public void hideToolBar() {
        //隐藏覆盖状态栏背景的控件
        mToolbar.setVisibility(View.GONE);
        findViewById(R.id.toolbar_split).setVisibility(View.GONE);
    }

    /**
     * 设置左边返回键图标
     *
     * @param drawable
     */
    public void setBackSrc(Drawable drawable) {
        mIvBack.setImageDrawable(drawable);
    }


    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        mTvTittle.setText(title);
    }
    /**
     * 设置标题
     *
     * @param resId
     */
    public void setTitle(@StringRes int resId) {
        mTvTittle.setText(resId);
    }

    /**
     * 方法说明：修改Title颜色
     *
     * @author yaohuix
     * @time 2016/11/28 17:03
     */
    public void setTitleColor(int color) {
        mTvTittle.setTextColor(color);

    }


    /**
    * 方法说明:  设置右边textview的图标
    * created by liuxiong on 2017/11/29 17:33
    */
    public void setRightTextIcon(@DrawableRes int drawId){
        TextView tvRight = findViewById(R.id.tv_right_text);
        DrawableUtil.setRightDrawable(tvRight,drawId);
    }

    public void setRightTextIcon(Drawable rightDrawable){
        TextView tvRight = findViewById(R.id.tv_right_text);
        tvRight.setCompoundDrawables(null, null, rightDrawable, null);
    }
    
    /**
    * 方法说明:  设置右边text的文字
    * created by liuxiong on 2017/11/29 17:04
    */
    public void setRightText(@StringRes int stringId){
        TextView tvRight = findViewById(R.id.tv_right_text);
        tvRight.setText(stringId);
    }

    public void setRightText(String string){
        TextView tvRight = findViewById(R.id.tv_right_text);
        tvRight.setText(string);
    }

    /**
    * 方法说明:  设置右边text的文字颜色
    * @param  colorId 颜色的资源id
    * created by liuxiong on 2017/12/2 17:00
    */
    public void setRightTextColorRes(@ColorRes int colorId){
        TextView tvRight = findViewById(R.id.tv_right_text);
        tvRight.setTextColor(ResourcesUtil.getColor(colorId));
    }
    /**
    * 方法说明:  设置右边text的文字颜色
    * @param  color 颜色值
    * created by liuxiong on 2017/12/2 17:01
    */
    public void setRightTextColor( int color){
        TextView tvRight = findViewById(R.id.tv_right_text);
        tvRight.setTextColor(color);
    }

    /**
    * 方法说明:  显示右边的按钮
    * @param isShow 是否显示
    * created by liuxiong on 2017/11/24 13:48
    */
    public void showOrHideRightMenu(boolean isShow){
        TextView tvRight = findViewById(R.id.tv_right_text);
        if(isShow){
            tvRight.setVisibility(View.VISIBLE);
        }else{
            tvRight.setVisibility(View.INVISIBLE);
        }

    }
    
    /**
    * 方法说明:  设置右边按钮的点击监听
    * created by liuxiong on 2017/11/24 14:10
    */
    public void setRightMenuClickListener(View.OnClickListener listener){
        TextView tvRight = findViewById(R.id.tv_right_text);
        tvRight.setOnClickListener(listener);
    }


    /***
     * 设置背景色
     *
     * @param color
     */
    public void setToolBarBackgroundColor(int color) {
        mToolbar.setBackgroundColor(color);
    }

    /***
     * 设置透明度
     */
    public void setToolBarAlpha(float i) {
        mToolbar.setAlpha(i);
    }

    /**
     * 设置背景图片
     *
     * @param drawable
     */
    public void setToolBarBackgroundDrawable(Drawable drawable) {
        mToolbar.setBackground(drawable);
    }

    /**
     * 隐藏toolbar下面那条间隙
     */
    public void hideToolBarBottomLine() {
        findViewById(R.id.toolbar_split).setVisibility(View.GONE);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
